﻿using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Entitys.BaseConfigs;
using HamcoWcs.Models.Common.Enums;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HamcoWcs.Service.Common
{
    [AutoInject(typeof(IMileageConfigService), InjectType.Single)]
    public class MileageConfigService : IMileageConfigService
    {
        private static string FileName = "MileageConfig.json";
        private static string DirName = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "MileageConfig");
        private string FilePath = Path.Combine(DirName, FileName);
        private Dictionary<string, int> _configDic = new Dictionary<string, int>();

        public MileageConfigService()
        {
            if (File.Exists(FilePath))
            {
                _configDic = JsonConvert.DeserializeObject<Dictionary<string, int>>(File.ReadAllText(FilePath));
            }
        }
        public int GetMileageConfig(string name)
        {
            if (!_configDic.ContainsKey(name))
            {
                _configDic.Add(name, 0);
            }
            return _configDic[name];
        }

        public void SaveMileageConfig()
        {
            if (!Directory.Exists(DirName))
            {
                Directory.CreateDirectory(DirName);
            }
            File.WriteAllText(FilePath, JsonConvert.SerializeObject(_configDic));
        }

        public void SetMileageConfig(string name, int value)
        {
            if (!_configDic.ContainsKey(name))
            {
                _configDic.Add(name, 0);
            }

            _configDic[name] = value;
        }
    }
}
